import config from "@/settings";
import { regUrl } from "@/utils/validate";

/**
 * @description 获取url中的参数
 * @param name -字段名
 * @returns {string}
 */
export const getUrlKey = name => {
  // const corpId = /corpId=([^=&]*)/.exec(window.location.href)[1]
  let url = window.location.href,
    urlInfo = url.split("?")[1],
    searchParams = new URLSearchParams("?" + urlInfo);
  return searchParams.get(name);
};

/**
 * @description 获取url
 * @param url
 * @return {*}
 */
export const delSearchParams = url => {
  if (regUrl(url)) {
    return url.includes("?") ? url.split("?")[0] : url;
  }
  return "";
};

/**
 * 对象参数转换查询字符串
 */
export const queryString = params => {
  return Object.keys(params)
    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
    .join("&");
};
